package ttelectronic.mobileholder.core;

import java.util.ArrayList;

import ttelectronic.mobileholder.R;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MHListDeviceAdapter extends BaseAdapter {

	private ArrayList<MHDeviceDetailObject> mListDevices;
	private Context mContext;

	public MHListDeviceAdapter(Context context,
			ArrayList<MHDeviceDetailObject> listDevices) {
		mContext = context;
		mListDevices = new ArrayList<MHDeviceDetailObject>();
		mListDevices.addAll(listDevices);
	}

	@Override
	public int getCount() {
		return mListDevices.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListDevices.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
		if (view == null) {
			LayoutInflater layoutInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.row_device_detail, null);
		}

		ImageView deviceImage = (ImageView) view
				.findViewById(R.id.ivDeviceImage);
		TextView deviceName = (TextView) view.findViewById(R.id.tvDeviceName);
		TextView deviceMA = (TextView) view
				.findViewById(R.id.tvDeviceMacAddress);

		MHDeviceDetailObject deviceDetailObject = (MHDeviceDetailObject) getItem(position);
		deviceName.setText(deviceDetailObject.getDeviceName());
		deviceMA.setText(deviceDetailObject.getDeviceMacAddress());
		switch (deviceDetailObject.getDeviceType()) {
		case BluetoothClass.Device.PHONE_SMART:
			deviceImage.setImageResource(R.drawable.ic_smart_phone);
			break;
		case BluetoothClass.Device.COMPUTER_LAPTOP:
			deviceImage.setImageResource(R.drawable.ic_laptop);
			break;
		default:
			deviceImage.setImageResource(R.drawable.ic_hardware);
			break;
		}
		return view;
	}
}
